import { request } from 'umi';

/** 一次性订单列表 */
export async function getOnceOrderList(
  params: {
    current?: number;
    pageSize?: number;
  }
) {
  return request(`/api/order/${params.current}/limit/${params.pageSize}`, {
    method: 'POST',
    data: {
      ...params,
    },
    headers: {
      Action_Id: '400400000'
    }
  });
}
// 一次性订单统计(400400011)
export async function getOnceOrderCount(params: string) {
  return request(`/api/order/count`, {
    method: 'POST',
    data: {
      ...params,
    },
    headers: {
      Action_Id: '400400011'
    }
  });
}



/** 一次性订单详情 */
export async function getOnceOrderDetail(order_id: string) {
  return request(`/api/order/${order_id}`, {
    method: 'GET',
    headers: {
      Action_Id: '400400002'
    }
  });
}

/** 一次性订单-修改订单状态(400400004,400400005) */
export async function updateOnceOrderStatus(data: {
  order_id: string,
  order_status: string;
}) {
  let actid = '';
  switch (data.order_status) {
    case 'E':                      // 已开单状态-取消
    case 'C': actid = '400400004'; // 取消支付-待支付订单修改前需要执行该操作
      break;
    case 'W': actid = '400400007'; // 发起支付
      break;
    case 'F': actid = '400400005'; // 完成
      break;
    case 'A': actid = '400400006'; // 申请退款
      break;
    default: actid = '';
      break;
  }
  return request(`/api/order/${data.order_id}/${data.order_status}`, {
    method: 'PATCH',
    data,
    headers: {
      Action_Id: actid,
    }
  });
}
// 无权限的取消支付
export const cancelOrder = (data) => {
  return request(`/api/order/${data.order_id}/${data.order_status}/cancel`, {
    method: 'PATCH',
    data,
    headers: {
      Action_Id: '400400004',
    }
  });
}

/** 一次性订单申请退款 */
export async function onceOrderRefund(data: {
  order_id: string;
  approve_status: string;
  cause?: string;
  refund_order_goods_list: STOCKGOODSAPI.GoodsItem[],
  refund_price: number,
  refund_service_price: number
}) {
  return request(`/api/order/${data.order_id}/refund`, {
    method: 'POST',
    data,
    headers: {
      Action_Id: '400400009'
    }
  });
}

/** 一次性订单修改 */
export async function updateOnceOrder(data: any) {
  return request(`/api/order/${data.order_id}`, {
    method: 'PATCH',
    data,
    headers: {
      Action_Id: '400400003'
    }
  });
}

/** 一次性订单-开单(400400001) */
export async function createOnceOrder(data: any) {
  return request(`/api/order/info`, {
    method: 'POST',
    data,
    headers: {
      Action_Id: '400400001'
    }
  });
}


/*** 计划订单接口 */
/** 计划订单列表 */
export async function getPlanOrderList(params: any) {
  return request(`/api/order/plan/${params.current}/limit/${params.pageSize}`, {
    method: 'POST',
    data: {
      ...params
    },
    headers: {
      Action_Id: '400401000'
    }
  });
}

/** 创建计划订单 */
export async function createPlanOrder(data: any) {
  return request(`/api/order/plan`, {
    method: 'POST',
    data,
    headers: {
      Action_Id: '400401001'
    }
  });
}

/** 费用预估 */
export async function predictFee(data: any) {
  return request(`/api/order/plan/predict`, {
    method: 'POST',
    data
  });
}

/** 删除计划订单 */
export async function deletePlanOrder(id: string) {
  return request(`/api/order/plan/${id}`, {
    method: 'DELETE',
    headers: {
      Action_Id: '400401003'
    }
  });
}

/** 计划订单详情
 * * 400401015 计划订单-打印明细权限
 */
export async function getPlanOrderDetail(id: string, flag?: boolean) {
  return request(`/api/order/plan/${id}`, {
    method: 'GET',
    headers: {
      Action_Id: !flag ? '400401002' : '400401004'
    }
  });
}

/** 办理出院
 *  办理出院(400401022)
 */
export async function handleDischarge(order_id: string) {
  return request(`/api/order/plan/${order_id}/finish`, {
    method: 'PATCH',
    headers: {
      Action_Id: '400401022'
    }
  });
}

/**
 * 查询打印商品明细
 */
export async function getFeeDetailGoodsList(data: { order_id?: string, current?: number, pageSize?: number }) {
  return request(`/api/order/plan/${data.order_id}/bill/data/${data.current}/limit/${data.pageSize}`, {
    method: 'GET',
    params: data,
    headers: {
      Action_Id: '400401015'
    }
  });
}

/** 保存核定量(400401020) */
export async function saveApprovedQuantity(data: any) {
  return request(`/api/order/plan/${data.order_id}/bill/${data.bill_id}/check/${data.check_num}`, {
    method: 'PATCH',
    data,
    headers: {
      Action_Id: '400401020'
    }
  });
}

/** 账单信息查询 400401012*/
export async function getBillList(params: any) {
  return request(`/api/order/plan/${params.order_id}/bill/${params.current}/limit/${params.pageSize}`, {
    method: 'GET',
    params,
    headers: {
      Action_Id: '400401012'
    }
  });
}

/** 查询结算列表 */
export async function getPlanSettleList(params: { order_id: string; current: number; pageSize: number; }) {
  return request(`/api/order/plan/${params.order_id}/sett/${params.current}/limit/${params.pageSize}`, {
    method: 'GET'
  });
}

/** 结算-查看详情(400401016) */
export async function getSettleDetail(data: any) {
  return request(`/api/order/plan/${data.order_id}/sett/${data.sett_id}`, {
    method: 'GET',
    headers: {
      Action_Id: '400401016'
    }
  });
}

/** 结算-查看-商品列表(400401016) */
export async function getSettleDetailGoodsList(data: any) {
  return request(`/api/order/plan/${data.order_id}/sett/${data.sett_id}/${data.current}/limit/${data.pageSize}`, {
    method: 'GET',
    headers: {
      Action_Id: '400401016'
    }
  });
}

/** 结算-编辑(400401017) */
export async function updateSettle(data: any) {
  return request(`/api/order/plan/${data.order_id}/sett/${data.sett_id}`, {
    method: 'POST',
    data,
    headers: {
      Action_Id: '400401017'
    }
  });
}

/** 结算/取消(400401018,400401019) */
export async function settleOrCancelSettle(data: { order_id: string; sett_id: string; sett_status: string; }) {
  return request(`/api/order/plan/${data.order_id}/sett/${data.sett_id}/${data.sett_status}`, {
    method: 'PATCH',
    data,
    headers: {
      Action_Id: data.sett_status === 'G' ? '400401018' : '400401019'
    }
  });
}

/** 计停结算(400401014) */
export async function stopSettle(data: { order_id: string }) {
  return request(`/api/order/plan/${data.order_id}/stop/sett`, {
    method: 'PATCH',
    headers: {
      Action_Id: '400401014'
    }
  });
}

/** 临时结算(400401013) */
export async function tempSettle(data: { order_id: string }) {
  return request(`/api/order/plan/${data.order_id}/temp/sett`, {
    method: 'PATCH',
    headers: {
      Action_Id: '400401013'
    }
  });
}

/** 项目计划列表 */
export async function getPlanProjectList(params: { order_id: string, current?: number; pageSize?: number }) {
  return request(`/api/order/plan/${params.order_id}/project/${params.current}/limit/${params.pageSize}`, {
    method: 'GET'
  });
}

/** 添加计划项目(400401006) */
export async function addPlanProject(data: any) {
  return request(`/api/order/plan/${data.order_id}/project`, {
    method: 'POST',
    data,
    headers: {
      Action_Id: '400401006'
    }
  });
}

/** 修改计划项目(400401021) */
export async function updatePlanProject(data: any) {
  return request(`/api/order/plan/${data.order_id}/project/${data.project_id}`, {
    method: 'PATCH',
    data,
    headers: {
      Action_Id: '400401021'
    }
  });
}

/** 批量停止计划项目(400401007) */
export async function batchStopPlanProject(order_id: string, ids: string[], end_time: string) {
  return request(`/api/order/plan/${order_id}/project/S/batch`, {
    method: 'PATCH',
    data: {
      project_ids: ids,
      end_time,
    },
    headers: {
      Action_Id: '400401007'
    }
  });
}

/** 修改计划项目状态(400401008,400401009,400401010) */
export async function updatePlanProjectStatus(params: {
  order_id: string;
  project_id: string;
  plan_status: string;
  begin_time?: string;
  end_time?: string;
}) {
  return request(`/api/order/plan/${params.order_id}/project/${params.project_id}/${params.plan_status}?end_time=${params.end_time}`, {
    method: 'PATCH',
    data: params,
    headers: {
      Action_Id: params.plan_status === 'D' ? '400401010' : params.plan_status === 'R' ? '400401008' : '400401009'
    }
  });
}



/**** 租赁订单接口  *****/
/** 租赁订单列表 */
export async function getOrderRentList(data: any) {
  return request(`/api/order/rent/${data.current}/limit/${data.pageSize}`, {
    method: 'POST',
    data,
    headers: {
      Action_Id: '400402000'
    }
  });
}

/** 按住院号查询租赁订单列表 */
export async function getOrderRentListByAdmission(data: any) {
  return request(`/api/order/rent/${data.current}/limit/${data.pageSize}/by/admission`, {
    method: 'POST',
    data,
    headers: {
      Action_Id: '400402000'
    }
  });
}

/** 租赁订单物品列表 */
export async function getOrderRentDetail(params: {
  current: number;
  pageSize: number;
  rent_order_id?: string;
  sett_id?: string;
  admission_num?: string;
}) {
  return request(`/api/order/rent/detail/${params.current}/limit/${params.pageSize}`, {
    method: 'GET',
    params
  });
}

/** 查询住院号下结算信息 */
export async function getRentSettleInfo(params: {
  current: number;
  pageSize: number;
  admission_num?: string;
}) {
  return request(`/api/order/rent/sett/${params.current}/limit/${params.pageSize}`, {
    method: 'GET',
    params
  });
}

/** 现结租赁 */
export async function rent(data: any) {
  return request(`/api/order/rent`, {
    method: 'POST',
    data,
    headers: {
      Action_Id: '400402002'
    }
  });
}

/** 挂账租赁 */
export async function hangRent(data: any) {
  return request(`/api/order/rent/hang`, {
    method: 'POST',
    data,
    headers: {
      Action_Id: '400402001'
    }
  });
}

/** 租赁结算详情 */
export async function getRentSettleDetail(sett_id: string) {
  return request(`/api/order/rent/sett/${sett_id}`, {
    method: 'GET',
    headers: {
      Action_Id: '400402009'
    }
  });
}

/** 挂帐租赁详情(400402004) */
export async function getRentDetailByRentOrderId(rent_order_id: string) {
  return request(`/api/order/rent/${rent_order_id}`, {
    method: 'GET',
    headers: {
      Action_Id: '400402004'
    }
  });
}

/** 结算信息-发起结算/取消结算(400402010,400402011) */
export async function updateSettleStatus(data: { sett_id: string; sett_status: string; }) {
  return request(`/api/order/rent/sett/${data.sett_id}/${data.sett_status}`, {
    method: 'PATCH',
    data,
    headers: {
      Action_Id: data.sett_status === 'U' ? '400402011' : '400402010'
    }
  });
}

/** 发起结算-所有未结算记录统计结算(400402007) */
export async function startSettle(data: { admission_num: string; }) {
  return request(`/api/order/rent/${data.admission_num}`, {
    method: 'PATCH',
    headers: {
      Action_Id: '400402007'
    }
  });
}

/** 删除物品信息(400402012) */
export async function deleteItem(id: string) {
  return request(`/api/order/rent/detail/${id}`, {
    method: 'DELETE',
    headers: {
      Action_Id: '400402012'
    }
  });
}

/** 订单支付(400402013) */
export async function rentPay(id: string) {
  return request(`/api/order/rent/${id}/pay`, {
    method: 'POST',
    headers: {
      Action_Id: '400402013'
    }
  });
}

/** 修改物品记录(400402008) */
export async function updateItem(data: any) {
  return request(`/api/order/rent/detail/${data.detail_id}`, {
    method: 'PATCH',
    data,
    headers: {
      Action_Id: '400402008'
    }
  });
}

/** 查询结算统计信息 */
export async function getRentSum(data: { admission_num?: string; rent_order_id?: string; }) {
  return request(`/api/order/rent/sum`, {
    method: 'GET',
    params: data,
    headers: {
      Action_Id: '400402008'
    }
  });
}


/** 批量修改归还状态 */
export async function updateRetureStatusBatch(data: any) {
  return request(`/api/order/rent/${data.rent_status}`, {
    method: 'PATCH',
    data,
    headers: {
      Action_Id: '400402003,400402008'
    }
  });
}

/**  创建患者服务档案 */
export async function createPatientArchive(data: any) {
  return request(`/api/order/worker`, {
    method: 'POST',
    data,
    headers: {
      Action_Id: '400403001'
    }
  });
}

/** 修改患者服务档案 */
export async function updatePatientArchive(data: any) {
  return request(` /api/order/worker/${data.order_id}`, {
    method: 'PATCH',
    data,
    headers: {
      Action_Id: '400403006'
    }
  });
}

/** 查询患者服务档案（400403002）
 * 400403007 患者服务档案-打印明细权限
 * 300300014 护工管理-档案
*/
export async function getPatientArchiveDetail(order_id: string, actid?: string) {
  return request(`/api/order/worker/${order_id}`, {
    method: 'GET',
    headers: {
      Action_Id: actid || '400403002'
    }
  });
}


/** 查询护工服务列表(400403000) */
export async function getWorkerServiceList(data: any) {
  return request(`/api/order/worker/${data.current}/limit/${data.pageSize}`, {
    method: 'POST',
    data,
    headers: {
      Action_Id: '400403000'
    }
  });
}

/** 查询服务列表 */
export async function getWorkerServiceListDetail(data: { order_id?: string; current?: number; pageSize?: number; }) {
  return request(`/api/order/worker/${data.order_id}/service/${data.current}/limit/${data.pageSize}`, {
    method: 'GET'
  });
}

/** 更改服务订单状态(400403005) */
export async function updateWorkerOrderStatus(data: { order_id: string; status: string; }, actid?: string) {
  return request(`/api/order/worker/${data.order_id}/${data.status}`, {
    method: 'PATCH',
    headers: {
      Action_Id: actid || '400403005'
    }
  });
}

/** 服务作废 (400403010)*/
export async function dropService(data: { order_id: string; service_id: string; status: string; }) {
  return request(`/api/order/worker/${data.order_id}/service/${data.service_id}/${data.status}`, {
    method: 'PATCH',
    headers: {
      Action_Id: '400403010'
    }
  });
}

/** 添加服务(400403008) */
export async function addService(data: any) {
  return request(`/api/order/worker/${data.order_id}/service`, {
    method: 'POST',
    data,
    headers: {
      Action_Id: '400403008'
    }
  });
}

/** 更新服务(400403009, 300300012) */
export async function updateService(data: any, actid) {
  return request(`/api/order/worker/${data.order_id}/service/${data.service_id}`, {
    method: 'PATCH',
    data,
    headers: {
      Action_Id: actid
    }
  });
}

/** 查询患者列表 */
export async function getPatientList(data: {
  identity_no?: string;
  ic_card_no?: string;
  patient_name?: string;
  tele_phone?: string;
}) {
  return request(`/api/user/list`, {
    method: 'GET',
    data,
    params: {
      ...data
    }
  });
}

/** 查询患者住院信息 */
export async function getPatientHospitalizationInfo(data: {
  patient_name?: string;
  visit_count?: string;
  patient_id?: string;
  ward_code?: string;
  bed_no?: string;
  out_visit_no?: string;
  out_case_no?: string;
}) {
  return request(`/api/user/hospital`, {
    method: 'GET',
    params: {
      ...data
    }
  });
}

/** 患者门诊就诊信息查询 */
export async function getOutpatientInformation(data: any) {
  return request(`/api/user/disp/visit`, {
    method: 'POST',
    data: data
  });
};

/** 查询办结账单列表-传入order_id则查询指定order_id账单列表(400404001,400404003 */
export async function getOffHospitalDetail(data: {
  patient_id?: string;
  admission_num: string;
  current: number;
  pageSize: number;
  order_id?: string;
  sett_no?: string;
}) {
  return request(`/api/order/manager/${data.patient_id}/and/${data.admission_num}/bill/${data.current}/limit/${data.pageSize}`, {
    method: 'GET',
    params: data,
    headers: {
      Action_Id: data.order_id ? '400404003' : '400404004'
    }
  });
}

/** 查询出院办结账单列表(400404000) */
export async function getOffHospitalBillList(data: {
  patient_id: string;
  admission_num: string;
  current: number;
  pageSize: number;
}) {
  return request(`/api/order/manager/${data.patient_id}/and/${data.admission_num}/${data.current}/limit/${data.pageSize}`, {
    method: 'GET',
    headers: {
      Action_Id: '400404000'
    }
  });
}

/** 查询结算信息列表 */
export async function getOffHospitalSettleList(data: any) {
  return request(`/api/order/manager/${data.patient_id}/and/${data.admission_num}/sett/${data.current}/limit/${data.pageSize}`, {
    method: 'GET'
  });
}

/** 申请出院办结(400404001) */
export async function applyOffHospital(data: {
  patient_id: string;
  admission_num: string;
}) {
  return request(`/api/order/manager/${data.patient_id}/and/${data.admission_num}`, {
    method: 'POST',
    headers: {
      Action_Id: '400404001'
    }
  });
}

/** 提交出院办结支付结算(400404006) */
export async function submitOffHospitalPay(manager_order_id: string) {
  return request(`/api/order/manager/${manager_order_id}/pay`, {
    method: 'POST',
    headers: {
      Action_Id: '400404006'
    }
  });
}

/** 取消支付(400404005) */
export async function cancalPay(manager_order_id: string) {
  return request(`/api/order/manager/${manager_order_id}/cancel`, {
    method: 'PATCH',
    headers: {
      Action_Id: '400404005'
    }
  });
}

/**查询出院办结状态 */
export async function getDischargeStatus(params) {
  return request(`/api/order/manager/status`, {
    method: 'GET',
    params
  });
}

// 打印汇总清单-查询办结账单汇总列表
export async function printOrderManagerCollect(params) {
  return request(`/api/order/manager/${params.patient_id}/and/${params.admission_num}/bill/collect/1/limit/99999`, {
    method: 'GET',
    params
  });
}
// 账单查询-出院结算可用
export async function printOrderPlanCollect(params) {
  return request(`/api/order/plan/${params.order_id}/project/bill/1/limit/99999`, {
    method: 'GET',
    params
  });
}

// 取消支付-供计划/一次性/租赁订单取消(出院办结)
export async function managerCancel(manager_order_id: string) {
  return request(`/api/order/manager/order/${manager_order_id}/cancel`, {
    method: 'PATCH',
  });
}


// 单独更新验光信息
export async function updateEyeInfo({ order_id, ...data }: any) {
  return request(`/api/order/${order_id}/eye`, {
    method: 'PATCH',
    data
  });
}

/** 中创订单列表 */
export async function getZcOrderList(
  params: {
    current?: number;
    pageSize?: number;
  }
) {
  return request(`/api/order/zc/${params.current}/limit/${params.pageSize}`, {
    method: 'POST',
    data: {
      ...params,
    },
    headers: {
      Action_Id: '400405000'
    }
  });
}


// 中创订单开单 （400405001）
export async function submitZcOrder(data: any) {
  return request(`/api/order/zc/info`, {
    method: 'POST',
    data,
    headers: {
      Action_Id: '400405001'
    }
  });
}

// 中创订单-查询详情(400405002）
export async function getZcOrderDetail(id: string) {
  return request(`/api/order/zc/${id}`, {
    method: 'GET',
    headers: {
      Action_Id: '400405002'
    }
  });
}


// 查询医生信息
export async function getEyeDoctorList() {
  return request(`/api/bui/doctor/1/limt/9999`);
}

// 新增医生信息。Y:验光师，D:就诊医生
export async function addEyeDoctor(doctor_name: string, doctor_type: 'Y' | 'D') {
  return request(`/api/bui/doctor/info`, {
    method: 'POST',
    data: { doctor_name, doctor_type }
  });
}

// 删除医生信息
export async function deleteEyeDoctor(doctor_id: string) {
  return request(`/api/bui/doctor/${doctor_id}`, {
    method: 'DELETE'
  });
}


// 获取镜片订单导出文件token
export async function getLensesOrderFileToken(begin_date_str: string, end_date_str: string, supplier?: string) {
  return request(`/api/data/goods/from/${begin_date_str}/to/${end_date_str}/spaces/report`, {
    method: 'GET',
    params: { supplier },
    headers: {
      Action_Id: '400400012'
    }
  });
}
